package com.haitaoit.chengquan.ui.equipment.request;

import com.github.retrofitutil.NetWorkManager;
import com.haitaoit.chengquan.business.request.PublicServiceReqSeconde;
import com.haitaoit.chengquan.ui.equipment.bean.SBWXAddRequirementBean;
import com.haitaoit.chengquan.ui.equipment.response.LoginBean;
import com.haitaoit.chengquan.ui.equipment.response.ResponseItem;
import com.haitaoit.chengquan.utils.Constant;
import com.haitaoit.chengquan.utils.MD5;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;

import static com.haitaoit.chengquan.utils.GetSign.exChange;

/**
 * Created by Administrator on 2017/5/17.
 */

public class ApiRequest {
    public static String getSign(String key,String value) {
        StringBuilder sb = new StringBuilder();
        sb.append(Constant.KEY);
        sb.append(key);
        sb.append(value);
        sb.append(Constant.KEY);
        String packageSign = MD5.getMessageDigest(sb.toString().getBytes())
                .toUpperCase();
        return exChange(packageSign);
    }
    public static String getSign (Map<String,String> params) {
        StringBuilder sb = new StringBuilder();
        sb.append(Constant.KEY);
        if(params instanceof HashMap){
            Object[] key_arr = params.keySet().toArray();
            Arrays.sort(key_arr);
            for  (Object key : key_arr) {
                sb.append(key);
                sb.append(params.get(key));
            }
        }else{
            for (Map.Entry<String, String> entry : params.entrySet()) {
                sb.append(entry.getKey());
                sb.append(entry.getValue());
            }
        }
        sb.append(Constant.KEY);
        String packageSign = MD5.getMessageDigest(sb.toString().getBytes())
                .toUpperCase();
        return exChange(packageSign);
    }
    private static Retrofit getClient(){
        return NetWorkManager.getGeneralClient();
    }
    public static void SBWXAddRequirement(String user,String sign,SBWXAddRequirementBean bean, final Callback<ResponseItem> callBack){
        Call<ResponseItem> call = getClient()
                .create(IRequest.class)
                .SBWXAddRequirement(user,sign,bean);
        call.enqueue(callBack);//异步请求
    }
    public static void userLogin(Map<String,String> map, final Callback<LoginBean> callBack){
        Call<LoginBean> call = getClient()
                .create(IRequest.class)
                .userLogin(map);
        call.enqueue(callBack);//异步请求
    }
    public static void getOrderCount(String userId,String sign, final Callback<LoginBean> callBack){
        Call call = getClient()
                .create(IRequest.class)
                .getOrderCount(userId,sign);
        call.enqueue(callBack);//异步请求
    }
    public static void ServePubliceRequirement(String user, String sign, PublicServiceReqSeconde bean, final Callback<ResponseItem> callBack){
        Call<ResponseItem> call = getClient()
                .create(IRequest.class)
                .ServePubliceRequirement(user,sign,bean);
        call.enqueue(callBack);//异步请求
    }
}
